package com.lanou.dllo.vc.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.chat.EMGroup;
import com.easemob.chat.EMGroupManager;
import com.easemob.exceptions.EaseMobException;
import com.lanou.dllo.vc.MyApplication;
import com.lanou.dllo.vc.R;
import com.lanou.dllo.vc.adapter.GroupDescAdapter;
import com.lanou.dllo.vc.view.EmotionGridview;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class GroupDescActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemLongClickListener {

    @Bind(R.id.group_desc_members)
    public EmotionGridview gridview;
    @Bind(R.id.group_descName)
    public TextView group_descName;
    @Bind(R.id.add_members)
    public  ImageView add_members;
    @Bind(R.id.minus_members)
    public  ImageView minus_members;
    private String groupId;
    private EMGroup group;
    private List<String> groupMembers = new ArrayList<>();
    private GroupDescAdapter adapter;

    private MyApplication application;
    private Handler handler;

    @Override
    protected int getLayout() {
        return R.layout.activity_group_desc;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        Intent intent = getIntent();
        if (intent != null) {
            groupId = intent.getStringExtra("groupID");
            Log.i("lanou", "三炮,看着-------->>" + groupId);
        } else {
            Toast.makeText(this, "你个三炮" + groupId, Toast.LENGTH_SHORT).show();
        }

        adapter = new GroupDescAdapter(this);

        gridview.setAdapter(adapter);

        add_members.setOnClickListener(this);
        minus_members.setOnClickListener(this);
        application = new MyApplication();
        // String myname = application.getUserName();
        // Log.i("TAG","我的名字"+myname);
        gridview.setOnItemLongClickListener(this);


    }

    @Override
    protected void initData() {

        //根据群聊ID从服务器获取群聊基本信息
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    group = EMGroupManager.getInstance().getGroupFromServer(groupId);
                    Log.i("lanou", "-------->>>" + group.getMembers().size());
                    groupMembers = group.getMembers();
//                    groupMembers.add("添加成员");
                    Log.d(TAG, "群主的名字" + group.getOwner());

//                    groupMembers.add("减少成员");
                    for (String groupMember : groupMembers) {
                        Log.i("lanou", "-------------->>>>" + groupMember);
                    }

                    adapter.addData(groupMembers);

                } catch (EaseMobException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if (msg.what == 19) {
                    initData();
//                    adapter.addData(groupMembers);

                } else if (msg.what == 30) {
                    initData();
                }
                return false;
            }
        });


    }

    public void back(View view) {
        finish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add_members:

                Intent intent = new Intent(this, FriendsListActivity.class);
                intent.putExtra("groupId", group.getGroupName());
                startActivityForResult(intent, 300);


                break;
            case R.id.minus_members:
                //  EMGroupManager.getInstance().removeUserFromGroup(groupId, username);
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 300) {

            if (data != null) {
                //添加新的成员
                final String[] newmembers = data.getStringArrayExtra("members");
                Log.i("lanou", newmembers + "天的成员");


                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            if (application.getUserName().equals(group.getOwner())) {
                                EMGroupManager.getInstance().addUsersToGroup(groupId, newmembers);
                                handler.sendEmptyMessage(30);

                            }
                        } catch (EaseMobException e) {
                            e.printStackTrace();
                        }
                    }

                }).start();

            }


        }
    }

    //减人
    String memberName = null;

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

        memberName = groupMembers.get(position);
        Log.i("lanou", memberName);
        if (memberName != null && memberName.length() > 0) {
            AlertDialog.Builder builder = new AlertDialog.Builder(GroupDescActivity.this);
            builder.setTitle("提示");
            builder.setMessage("确定删除吗?");
            builder.setPositiveButton("删除", new DialogInterface.OnClickListener() {


                @Override
                public void onClick(DialogInterface dialog, int which) {

                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                EMGroupManager.getInstance().removeUserFromGroup(groupId, memberName);//需异步处理
                                handler.sendEmptyMessage(19);

                            } catch (EaseMobException e) {
                                e.printStackTrace();
                            }
                        }
                    }).start();

                }
            });
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            builder.create().show();
        }


        return false;
    }
}
